package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DiagnoseMapper;
import com.ruoyi.system.domain.Diagnose;
import com.ruoyi.system.service.IDiagnoseService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 诊断Service业务层处理
 *
 * @author lwb
 * @date 2024-08-21
 */
@Service
public class DiagnoseServiceImpl implements IDiagnoseService {
    @Resource
    private DiagnoseMapper diagnoseMapper;

    /**
     * 查询诊断
     *
     * @param id 诊断主键
     * @return 诊断
     */
    @Override
    public Diagnose selectDiagnoseById(Long id) {
        return diagnoseMapper.selectDiagnoseById(id);
    }

    /**
     * 查询诊断列表
     *
     * @param diagnose 诊断
     * @return 诊断
     */
    @Override
    public List<Diagnose> selectDiagnoseList(Diagnose diagnose) {
        return diagnoseMapper.selectDiagnoseList(diagnose);
    }

    /**
     * 新增诊断
     *
     * @param diagnose 诊断
     * @return 结果
     */
    @Override
    public int insertDiagnose(Diagnose diagnose) {
        diagnose.setCreateTime(DateUtils.getNowDate());
        return diagnoseMapper.insertDiagnose(diagnose);
    }

    /**
     * 修改诊断
     *
     * @param diagnose 诊断
     * @return 结果
     */
    @Override
    public int updateDiagnose(Diagnose diagnose) {
        diagnose.setUpdateTime(DateUtils.getNowDate());
        return diagnoseMapper.updateDiagnose(diagnose);
    }

    /**
     * 批量删除诊断
     *
     * @param ids 需要删除的诊断主键
     * @return 结果
     */
    @Override
    public int deleteDiagnoseByIds(Long[] ids) {
        return diagnoseMapper.deleteDiagnoseByIds(ids);
    }

    /**
     * 删除诊断信息
     *
     * @param id 诊断主键
     * @return 结果
     */
    @Override
    public int deleteDiagnoseById(Long id) {
        return diagnoseMapper.deleteDiagnoseById(id);
    }

    @Override
    public String importCase(List<Diagnose> diagnoseList, boolean updateSupport, String operName) {

        if (CollUtil.isEmpty(diagnoseList)) {
            throw new ServiceException("导入数据不能为空！");
        }

        AtomicInteger index = new AtomicInteger(2);

        String username = getUsername();
        diagnoseList.forEach(item -> {
            if (StrUtil.isBlank(item.getCode())) {
                throw new ServiceException("第" + index.get() + "行的诊断编码不能为空！");
            }
            if (StrUtil.isBlank(item.getName())) {
                throw new ServiceException("第" + index.get() + "行的诊断名称不能为空！");
            }

            Date nowDate = DateUtils.getNowDate();
            item.setCreateTime(nowDate);
            item.setCreateBy(username);
            item.setUpdateTime(nowDate);
            item.setUpdateBy(username);


            index.getAndIncrement();
        });
        //删除数据库数据
         diagnoseMapper.delete();
         diagnoseMapper.saveBatch(diagnoseList);
        return "导入成功";
    }

    @Override
    public List<Diagnose> listByName(List<String> names) {
        if (CollUtil.isEmpty(names)){
            return CollUtil.newArrayList();
        }
        return diagnoseMapper.listByName(names);
    }
}
